# Contributor: Pedro Filipe <xpecex@outlook.com>
# Contributor: Stefan Wagner <stw@bit-strickerei.de>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=gcc-avr
pkgver=14.2.0
pkgrel=0
pkgdesc="The GNU Compiler Collection for AVR targets"
url="https://gcc.gnu.org/"
depends="binutils-avr"
makedepends="bash linux-headers gmp-dev mpfr-dev mpc1-dev zlib-dev isl-dev"
arch="x86_64 aarch64 ppc64le x86"
license="GPL-3.0-or-later"
subpackages="$pkgname-doc"
source="
	https://mirrors.kernel.org/gnu/gcc/gcc-$pkgver/gcc-$pkgver.tar.xz

	fix-no-system-headers.patch
	"

builddir="$srcdir/gcc-$pkgver"
_workingdir="$builddir/build"

build() {
	mkdir "$_workingdir"
	cd "$_workingdir"

	export CFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections'
	export CXXFLAGS_FOR_TARGET='-pipe -Os -ffunction-sections -fdata-sections -fno-use-cxa-atexit'

	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100431
	export CFLAGS="$CFLAGS -Wno-error=format-security"
	export CXXFLAGS="$CXXFLAGS -Wno-error=format-security"
	export CPPFLAGS="$CPPFLAGS -Wno-error=format-security"

	"$builddir"/configure \
		--target=avr \
		--prefix=/usr \
		--with-sysroot=/usr/avr \
		--with-native-system-header-dir=/include \
		--with-headers=/usr/avr/include \
		--with-python-dir=share/gcc-avr \
		--libexecdir=/usr/lib \
		--enable-languages=c,c++ \
		--disable-__cxa_atexit \
		--disable-install-libiberty \
		--disable-libssp \
		--disable-libstdcxx-pch \
		--disable-libunwind-exceptions \
		--disable-linker-build-id \
		--disable-nls \
		--disable-werror \
		--with-gnu-as \
		--with-gnu-ld \
		--with-system-zlib \
		--with-isl \
		--with-avrlibc \
		--with-pkgversion='Alpine Linux'
	make
}

package() {
	cd "$_workingdir"
	make install DESTDIR="$pkgdir"
	rm -rf "$pkgdir"/usr/share/man/man7
	rm -f "$pkgdir"/usr/lib/libcc1.*
	find "$pkgdir"/usr/lib -type f -name "*.a" -exec /usr/bin/avr-strip --strip-debug '{}' \;
}

sha512sums="
932bdef0cda94bacedf452ab17f103c0cb511ff2cec55e9112fc0328cbf1d803b42595728ea7b200e0a057c03e85626f937012e49a7515bc5dd256b2bf4bc396  gcc-14.2.0.tar.xz
99bbe52c3c411e5a5af47a2539524f036190ab34de2eff762554c65936ee9ed713b91fc605bcbc5f11e740580356748d40c520d915687a77ea81ae3ef31bfb0d  fix-no-system-headers.patch
"
